<?php
namespace app\admin\controller;

class Authgroup extends Admin{
    public function __construct(){
        parent::__construct();
        $this->model = 'AuthGroup';
        $this->assign("model","AuthGroup");
    }
    public function data_delete(){
        $items = input("param.items");
        if(strpos($items,"1,") !== false){
            return json(['status' => 0,'msg'=>'超级管理员不允许删除']);
        }
        $items = trim($items,",");
        $model = input("param.model");

        $db = model($model);
        $db->delete_data(['item_ids' => $items]);
        
        return json(['status' => 1,'msg'=>'操作成功']);
    }
    public function setrule(){
        $id = input("param.id");
        $id = trim($id,",");
        $list = db("Menu")->where("pid",0)->order("id")->select();
        $menu = [];
        foreach($list as $k => $v){
            $menu[$k]['title'] = $v['title'];
            $menu[$k]['id'] = $v['id']; 
            $menu[$k]['child'] = [];
            $childlist = db("Menu")->where("pid",$v['id'])->order("id")->select();
            if(!empty($childlist)){
                $menu[$k]['child'] = $childlist; 
                foreach($childlist as $ks => $vs){
                    $menu[$k]['child'][$ks]['title'] = $vs['title'];
                    $menu[$k]['child'][$ks]['id'] = $vs['id'];
                    $menu[$k]['child'][$ks]['child'] = db("Menu")->where("pid",$vs['id'])->order("id")->select();
                }
            }
        }
        $rule = model("AuthGroup")->where('id', $id)->value('rules');
        $rule = explode(',', $rule);
        trace($rule);
        $this->assign('rule', $rule);
        $this->assign("roleid",$id);
        $this->assign("menu",$menu);
        return $this->fetch();
    }
    public function setRuleAct(){
        $postData = input("post.postData");
        parse_str($postData,$dataarr);
        $roleid = $dataarr['roleid'];
        $rule = $dataarr['rule'];
        $rules = implode(",",$rule);
        db("AuthGroup")->where("id = '$roleid'")->setField("rules",$rules);
        return json(['status' => 1,'msg'=>'操作成功']);
    }
}